home *** CD-ROM | disk | FTP | other *** search
/ AMIGA-CD 2 / Amiga-CD - Volume 2.iso / gepackte_disketten / 1993 / 06_93_2.dms / 06_93_2.adf / Timer-Programmierung / timer.c < prev    next >
C/C++ Source or Header  |  1993-05-20  |  7KB  |  284 lines

  1. ; /*
  2. sc streq shortint strmerge link map timer
  3. quit
  4. */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/libraries.h>
  8. #include <exec/memory.h>
  9. #include <graphics/gfxbase.h>
  10. #include <graphics/gfx.h>
  11. #include <graphics/gfxmacros.h>
  12. #include <devices/timer.h>
  13. #include <libraries/gadtools.h>
  14. #include <utility/date.h>
  15. #include <intuition/intuitionbase.h>
  16. #include <proto/exec.h>
  17. #include <proto/graphics.h>
  18. #include <proto/timer.h>
  19. #include <proto/intuition.h>
  20. #include <proto/gadtools.h>
  21. #include <proto/utility.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. #define    AMIGAOS_V_37    37
  27.  
  28. extern struct Library *GadToolsBase;
  29. extern struct Library *UtilityBase;
  30. extern struct IntuitionBase *IntuitionBase;
  31. extern struct GfxBase *GfxBase;
  32.  
  33. struct Library *TimerBase;
  34. struct MsgPort *timer_mp;
  35. struct timerequest *timer_io;
  36.  
  37. static void DisposeTimerIO(int n) {
  38.     switch(n) {
  39.         case 0:  CloseDevice(timer_io);
  40.         case 1:    DeleteIORequest(timer_io);
  41.         case 2:     DeleteMsgPort(timer_mp);
  42.         default:    break;
  43.     }
  44. }
  45.  
  46. static int InitTimerIO(void) {
  47.     if(timer_mp = CreateMsgPort()) {
  48.         if(timer_io = CreateIORequest
  49.         (timer_mp,sizeof(struct timerequest))) {
  50.             if(OpenDevice(TIMERNAME,
  51.             UNIT_VBLANK,timer_io,0L) == 0) {
  52.                 TimerBase = (struct Library*)
  53.                 timer_io->tr_node.io_Device;
  54.                 return(TRUE);
  55.             } else DisposeTimerIO(1);
  56.         } else DisposeTimerIO(2);
  57.     } else DisposeTimerIO(3);
  58.     return(FALSE);
  59. }
  60.  
  61. #define    ID_START    1
  62. #define    ID_STOP    2
  63. #define    ID_RESET    3
  64.  
  65. void DisposeWindow(struct Window *w) {
  66.     struct Gadget *g = w->FirstGadget;
  67.     CloseWindow(w);
  68.     FreeGadgets(g);
  69. }
  70.  
  71. #define    G_WIDTH        100
  72. #define    G_HEIGHT        30
  73. #define    B_TOP(w)        ((w)->BorderTop + 5 + G_HEIGHT)
  74. #define    B_LEFT(w)    ((w)->BorderLeft + 3)
  75. #define    B_HEIGHT(w)    ((w)->Height - (w)->BorderTop -\
  76.                             (w)->BorderBottom - G_HEIGHT - 7)
  77. #define    B_WIDTH(w)    ((w)->Width - (w)->BorderLeft -\
  78.                             (w)->BorderRight - 6)
  79.  
  80. struct TextAttr taPlain = {    "topaz.font",TOPAZ_EIGHTY    };
  81.  
  82. struct Window *InitWindow(void) {
  83.     APTR vi;    struct NewGadget ng;
  84.     struct Screen *ps;    struct Window *w;
  85.     struct Gadget *gl,*gad = NULL;
  86.  
  87.     if(ps = LockPubScreen(NULL)) {
  88.         if(vi = GetVisualInfoA(ps,NULL)) {
  89.             if(gad = CreateContext(&gl)) {
  90.                 ng.ng_LeftEdge        = ps->WBorLeft + 3;
  91.                 ng.ng_TopEdge        = ps->BarHeight + 4;
  92.                 ng.ng_Width            = G_WIDTH;
  93.                 ng.ng_Height        = G_HEIGHT;
  94.                 ng.ng_VisualInfo    = vi;
  95.                 ng.ng_GadgetText    = "Start";
  96.                 ng.ng_TextAttr        = &taPlain;
  97.                 ng.ng_Flags            = PLACETEXT_IN;
  98.                 ng.ng_GadgetID        = ID_START;
  99.                 ng.ng_UserData        = NULL;
  100.                 gad = CreateGadget(BUTTON_KIND,gad,&ng,TAG_DONE);
  101.  
  102.                 ng.ng_LeftEdge        = gad->LeftEdge + gad->Width + 1;
  103.                 ng.ng_GadgetText    = "Stop";
  104.                 ng.ng_Flags            = PLACETEXT_IN;
  105.                 ng.ng_GadgetID        = ID_STOP;
  106.                 gad = CreateGadget(BUTTON_KIND,gad,&ng,TAG_DONE);
  107.  
  108.                 ng.ng_LeftEdge        = gad->LeftEdge + gad->Width + 1;
  109.                 ng.ng_GadgetText    = "Reset";
  110.                 ng.ng_Flags            = PLACETEXT_IN;
  111.                 ng.ng_GadgetID        = ID_RESET;
  112.                 gad = CreateGadget(BUTTON_KIND,gad,&ng,TAG_DONE);
  113.  
  114.                 if(w = OpenWindowTags(NULL,
  115.                 WA_Flags,(ULONG)WFLG_CLOSEGADGET
  116.                 |WFLG_DEPTHGADGET|WFLG_DRAGBAR|WFLG_ACTIVATE,
  117.                 WA_IDCMP,(ULONG)IDCMP_GADGETUP|IDCMP_CLOSEWINDOW,
  118.                 WA_InnerHeight,(ULONG)G_HEIGHT + 40L,
  119.                 WA_InnerWidth,(ULONG)G_WIDTH * 3 + 8,
  120.                 WA_Gadgets,gl,
  121.                 WA_Title,"stop watch",
  122.                 TAG_DONE)) {
  123.  
  124.                     GT_RefreshWindow(w,NULL);
  125.  
  126.                     SetAPen(w->RPort,1);
  127.                     SetBPen(w->RPort,0);
  128.                     SetDrMd(w->RPort,JAM2);
  129.  
  130.                     DrawBevelBox(w->RPort,B_LEFT(w),
  131.                     B_TOP(w),B_WIDTH(w),B_HEIGHT(w),
  132.                     GT_VisualInfo,vi,GTBB_Recessed,1L,TAG_DONE);
  133.                 }
  134.                 else FreeGadgets(gl);
  135.             }
  136.             FreeVisualInfo(vi);
  137.         }
  138.         UnlockPubScreen(NULL,ps);
  139.     }
  140.     return(w);
  141. }
  142.  
  143. int SendTimerIO(int type,ULONG secs,ULONG micro) {
  144.     struct timerequest *tr;
  145.     if(tr = (struct timerequest*)AllocMem
  146.     (sizeof(struct timerequest),MEMF_PUBLIC)) {
  147.         *tr = *timer_io;
  148.         tr->tr_node.io_Command = type;
  149.         tr->tr_time.tv_secs = secs;
  150.         tr->tr_time.tv_micro = micro;
  151.         SendIO(&tr->tr_node);
  152.         return(1);
  153.     }
  154.     return(0);
  155. }
  156.  
  157. void CloseLibs(int n) {
  158.     switch(n) {
  159.         case 0:    CloseLibrary(&GfxBase->LibNode);
  160.         case 1:    CloseLibrary(UtilityBase);
  161.         case 2:    CloseLibrary(GadToolsBase);
  162.         case 3:    CloseLibrary(&IntuitionBase->LibNode);
  163.         default:    break;
  164.     }
  165. }
  166.  
  167. int OpenLibs(void) {
  168.     if(IntuitionBase = (struct IntuitionBase*)
  169.     OpenLibrary("intuition.library",AMIGAOS_V_37)) {
  170.         if(GadToolsBase = OpenLibrary
  171.         ("gadtools.library",AMIGAOS_V_37)) {
  172.             if(UtilityBase = OpenLibrary
  173.             ("utility.library",AMIGAOS_V_37)) {
  174.                 if(GfxBase = (struct GfxBase*)
  175.                 OpenLibrary("graphics.library",AMIGAOS_V_37)) {
  176.                     return(1);
  177.                 } else CloseLibs(1);
  178.             } else CloseLibs(2);
  179.         } else CloseLibs(3);
  180.     }
  181.     return(0);
  182. }
  183.  
  184. #define    DEF_TIMEOUT        50000L
  185.  
  186. void _main(char *line) {
  187.     struct IntuiMessage *imsg;        struct timerequest *tr;
  188.     struct Window *w;                    struct ClockData cd0,cd1;
  189.     int outReq = 0, done = FALSE, stpRun = FALSE;
  190.  
  191.     if(OpenLibs()) {
  192.  
  193.         if(w = InitWindow()) {
  194.  
  195.             if(InitTimerIO()) {
  196.  
  197.                 outReq += SendTimerIO(TR_ADDREQUEST,0L,DEF_TIMEOUT);
  198.  
  199.                 while(!done || outReq) {
  200.                     static struct timeval tv0,tv1;
  201.                     ULONG sigRcvd;
  202.  
  203.                     sigRcvd = Wait(1L << timer_mp->mp_SigBit
  204.                     | 1L << w->UserPort->mp_SigBit);
  205.  
  206.                     timer_io->tr_node.io_Command = TR_GETSYSTIME;
  207.                     DoIO(&timer_io->tr_node);
  208.  
  209.                     if(sigRcvd & (1L << w->UserPort->mp_SigBit)) {
  210.                         ULONG im_code,im_class;
  211.                         APTR im_address;
  212.  
  213.                         while(imsg = GT_GetIMsg(w->UserPort)) {
  214.                             im_class = imsg->Class;
  215.                             im_code = imsg->Code;
  216.                             im_address = imsg->IAddress;
  217.                             GT_ReplyIMsg(imsg);
  218.                             switch(im_class) {
  219.                                 case IDCMP_GADGETUP:
  220.                                     switch(((struct Gadget*)
  221.                                     im_address)->GadgetID) {
  222.                                         case ID_START:    stpRun = TRUE;        break;
  223.                                         case ID_STOP:    stpRun = FALSE;    break;
  224.                                         case ID_RESET:    tv0 = tv1 = timer_io->tr_time;
  225.                                                             SubTime(&tv1,&tv0);
  226.                                                             break;
  227.                                         default:            break;
  228.                                     }
  229.                                     break;
  230.                                 case CLOSEWINDOW:        done = TRUE;    break;
  231.                                 default:                    break;
  232.                             }
  233.                         }
  234.                     }
  235.                     if(stpRun == FALSE) {
  236.                         struct timeval tvdiff = timer_io->tr_time;
  237.                         SubTime(&tvdiff,&tv0);    SubTime(&tvdiff,&tv1);
  238.                         AddTime(&tv0,&tvdiff);
  239.                     }
  240.  
  241.                     tv1 = timer_io->tr_time;
  242.                     SubTime(&tv1,&tv0);
  243.  
  244.                     if(sigRcvd & (1L << timer_mp->mp_SigBit)) {
  245.                         static ULONG oldTSec = 0,oldSSec = 0;
  246.                         UBYTE t_string[32];
  247.  
  248.                         while(tr = (struct timerequest*)
  249.                         GetMsg(timer_mp)) {
  250.                             outReq--;
  251.                             FreeMem((UBYTE*)tr,sizeof(struct timerequest));
  252.  
  253.                             if(oldTSec < timer_io->tr_time.tv_secs
  254.                             || oldSSec < tv1.tv_secs) {
  255.                                 oldSSec = tv1.tv_secs;
  256.                                 oldTSec = timer_io->tr_time.tv_secs;
  257.                                 Amiga2Date(timer_io->tr_time.tv_secs,&cd0);
  258.                                 Amiga2Date(tv1.tv_secs,&cd1);
  259.                                 sprintf(t_string,
  260.                                 "%02d:%02d:%02d - %02d:%02d:%02d",
  261.                                 cd0.hour,cd0.min,cd0.sec,
  262.                                 cd1.hour,cd1.min,cd1.sec);
  263.                                 Move(w->RPort,B_LEFT(w) + (B_WIDTH(w)
  264.                                 - TextLength(w->RPort,t_string,
  265.                                 strlen(t_string))) / 2,
  266.                                 B_TOP(w) + (B_HEIGHT(w)
  267.                                 + w->RPort->Font->tf_Baseline) / 2);
  268.                                 Text(w->RPort,t_string,strlen(t_string));
  269.                             }
  270.                             if(!done) {
  271.                                 outReq += SendTimerIO
  272.                                 (TR_ADDREQUEST,0L,DEF_TIMEOUT);
  273.                             }
  274.                         }
  275.                     }
  276.                 }
  277.                 DisposeTimerIO(0);
  278.             }
  279.             DisposeWindow(w);
  280.         }
  281.         CloseLibs(0);
  282.     }
  283. }
  284.